<?php
class visitruleController extends AController {
	private $tableName = 'VISIT_RULE';
	
	public function indexAction(){
		$page = $this->request->getGetParameter('p',1);
		$pagesize = $this->request->getGetParameter('ps',10);
		$sel['title'] = $rulename = $this->request->getGetParameter('n');
		$sel['state'] = $state = $this->request->getGetParameter('st');
		if(isset($_POST['sub_sel'])){
			$sel['title'] = $rulename = $this->request->getPostParameter('r_name');
			$sel['state'] = $state = $this->request->getPostParameter('r_state');
		}
		$tolrule = $this->getAllRule($page,$pagesize,$sel);
		$tolNum = $this->getRuleTolNum($sel);
		$allDep = $this->getAllDep();
		$allDoc = $this->getAllDoc();
		$allQuesnr = $this->getAllQuesnr();
		foreach ($allDep as $v){
			$dep[$v['DID']] = $v['DN'];
		}
		foreach ($allDoc as $v){
			$doc[$v['DID']] = $v['DN'];
		}
		foreach ($allQuesnr as $v){
			$quesnr[$v['QID']] = $v['QN'];
		}
		unset($allDep);
		unset($allDoc);
		unset($allQuesnr);
		$this->page = $page;
		$this->pagesize = $pagesize;
		$this->rule = $rulename;
		$this->state = $state;
		$this->allRule = $tolrule;
		$this->tolNum = $tolNum[0]['NUM'];
		$this->allDep = $dep;
		$this->allDoc = $doc;
		$this->allQuesnr = $quesnr;
		return $this->render();
	}
	public function editAction(){
		$id = $this->request->getGetParameter('id');
		$qid = $this->request->getGetParameter('qid');
		if(isset($_POST['sub_add'])){
			$edit['TITLE']  = $title = $this->request->getPostParameter('t_name');
			$edit['DESCRPITION']  = $remark = $this->request->getPostParameter('t_info');
			$edit['QUES_ID']  = $qid = $this->request->getPostParameter('t_quesnr');
			$edit['STATE'] = 1;
			$edit['UPD_TIME'] = date('Y-m-d H:i:s');
			$edit['OLD_QUES_IDS'] = $this->request->getPostParameter('t_quesnr').','.$qid;
			$edit['UPD_USER'] = $this->username;
			$key = $this->request->getPostParameter('key');
			if($key == md5($title.$qid.$remark)){
				$this->alert('您没有修改任何东西');exit;
			}
			if($this->editVisitrule($id,$edit)){
				$this->alert('操作成功','/index.php?c=visitrule&a=edit&id='.$id);exit;
			}
			$this->alert('系统繁忙，请重试');exit;
		}
		$ruleInfo = $this->getRuleInfoById($id);
		if(!empty($qid)){
			$quesnrInfo = $this->getQuesnrInfoById($qid);
		}else{
			$quesnrInfo = $this->getQuesnrInfoById($ruleInfo['QUES_ID']);
		}
		$allDep = $this->getAllDep();
		$allDoc = $this->getAllDoc();
		$keys = md5($ruleInfo['TITLE'].$ruleInfo['QUES_ID'].$ruleInfo['DESCRPITION']);
		$this->doc = $allDoc;
		$this->dep = $allDep;
		$this->id = $id;
		$this->keys = $keys;
		$this->ruleInfo = $ruleInfo;
		$this->quesnrinfo = $quesnrInfo;
		return $this->render('../tpl/visitrule/add.php');
	}
	public function addAction(){
		$qid = $this->request->getGetParameter('qid');
		if(!empty($qid)){
			$quesnrInfo = $this->getQuesnrInfoById($qid);
		}
		if(isset($_POST['sub_add'])){
			$add['TITLE'] = $this->request->getPostParameter('t_name');
			if(empty($add['TITLE'])){
				$this->alert('标题不能为空');exit;
			}
			$add['RLEVEL'] = $level = $this->request->getPostParameter('t_level');
			switch ($level){
				case '2':$add['DEP_NO']=$this->request->getPostParameter('t_dep');break;
				case '3':$add['DOC_NO']=$this->request->getPostParameter('t_doc');break;
				default:break;
			}
			$add['R_ID'] = $this->request->getPostParameter('t_code');
			$add['URL'] = $this->request->getPostParameter('t_url');
			$add['QUES_ID'] = $this->request->getPostParameter('t_quesnr');
			$add['DESCRPITION'] = $this->request->getPostParameter('t_info');
			$add['STATE'] = 1;
			$add['UPD_TIME'] = date('Y-m-d H:i:s');
			$add['UPD_USER'] = $this->username;
			$add['UNIT_ID'] = $this->unid;
			$add['OLD_QUES_IDS'] = $this->request->getPostParameter('t_quesnr');
			//var_dump($add);exit;
			if($this->addVisitrule($add)){
				$this->alert('操作成功');exit;
			}
			$this->alert('系统繁忙，请重试');exit;
		}
		$allDep = $this->getAllDep();
		$allDoc = $this->getAllDoc();
		$this->quesnrinfo = $quesnrInfo;
		$this->doc = $allDoc;
		$this->dep = $allDep;
		return $this->render();
	}
	public function delAction(){
		$id = $this->request->getGetParameter('id');
		if($this->delRule($id)){
			$this->alert('操作成功');exit;
		}
		$this->alert('系统繁忙，请重试');exit;
		return $this->render();
	}
	public function sysAction(){
		$page = $this->request->getGetParameter('p',1);
		$pagesize = $this->request->getGetParameter('ps',8);
		$select['dep_id'] = $did = JYFilterUtils::unfilterText($this->request->getGetParameter('did'));
		$select['cat'] = $cid = JYFilterUtils::unfilterText($this->request->getGetParameter('cid'));
		$select['title'] = $title = JYFilterUtils::unfilterText($this->request->getGetParameter('title'));
		if(isset($_POST['sub_sel'])){
			$select['dep_id'] = $did = JYFilterUtils::unfilterText($this->request->getPostParameter('did'));
			$select['cat'] = $cid = JYFilterUtils::unfilterText($this->request->getPostParameter('cid'));
			$select['title'] = $title = JYFilterUtils::unfilterText($this->request->getPostParameter('title'));
		}
		
		$allQuesnr = $this->getAllQuesnrs($page,$pagesize,$select);
		$tolNum = $this->getQuesnrTotalNum($select);
		$alldep = $this->getAllDep();
		$allcate = $this->getQuesnrCategory();
		foreach ($alldep as $v){
			$dep[$v['DID']] = $v['DN'];
		}
		foreach ($allcate as $v){
			$cate[$v['CID']] = $v['CN'];
		}
		unset($alldep);
		unset($allcate);
		$this->did = $did;
		$this->cid = $cid;
		$this->title = $title;
		$this->dep = $dep;
		$this->cate = $cate;
		$this->page = $page;
		$this->pagesize = $pagesize;
		$this->tolNum = $tolNum[0]['NUM'];
		$this->allquesnr = $allQuesnr;
		return $this->render();
	}
	public function closeAction(){
		$id = $this->request->getGetParameter('id');
		if($this->optionRule($id,2)){
			$this->redirect('/index.php?c=visitrule&a=index');
			exit;
		}
		$this->alert('系统繁忙，请重试');exit;
	}
	public function openAction(){
		$id = $this->request->getGetParameter('id');
		if($this->optionRule($id,1)){
			$this->redirect('/index.php?c=visitrule&a=index');
			exit;
		}
		$this->alert('系统繁忙，请重试');exit;
	}
	function getAllRule($page,$pagesize,$select){
		if($this->unid<0){
			$where = 'state>0';
		}else{
			$where = 'state>0 and unit_id='.$this->unid;
		}
		
		if(count($select)>0){
			foreach ($select as $k=>$v){
				if(!empty($v)){
					if($k=='title'){
						$where .= ' and '.$k.' like \'%'.$v.'%\'';
						continue;
					}
					$where .= ' and '.$k.'='.$v;
				}
			}
		}
//		echo $where;
		$order = 'state,upd_time desc';
		return JYDB::findAll($this->tableName,$where,'*',$order,$page,$pagesize);
	}
	function getRuleTolNum($select){
		if($this->unid<0){
			$where = 'state>0';
		}else{
			$where = 'state>0 and unit_id='.$this->unid;
		}
		if(count($select)>0){
			foreach ($select as $k=>$v){
				if(!empty($v)){
					if($k=='title'){
						$where .= ' and '.$k.' like \'%'.$v.'%\'';
						continue;
					}
					$where .= ' and '.$k.'='.$v;
				}
			}
		}
		return JYDB::findAll($this->tableName,$where,'count(*) num');
	}
	function getAllDep(){
		if($this->unid<0){
			$where = '';
		}else{
			$where = 'unit_id='.$this->unid;
		}
		return JYDB::findAll('DEP',$where,'DEP_ID DID,DEP_NAME DN');
	}
	function getAllDoc(){
		if($this->unid<0){
			$where = '';
		}else{
			$where = 'unit_id='.$this->unid;
		}
		return JYDB::findAll('DOCTOR',$where,'DOCTOR_ID DID,DOCTOR_NAME DN');
	}
	function getAllQuesnr(){
		if($this->unid<0){
			$where = '';
		}else{
			$where = 'unit_id='.$this->unid;
		}
		return JYDB::findAll('QUESTIONNAIRE',$where,'QUES_ID qid,TITLE QN');
	}
	function getAllQuesnrs($page,$pagesize,$select){
		if($this->unid<0){
			$where = 'state=3';
		}else{
			$where = 'state=3 and unit_id='.$this->unid;
		}
		
		if(count($select)>0){
			foreach ($select as $k=>$v){
				if(!empty($v)){
					if($k=='title'){
						$where .= ' and '.$k.' like \'%'.$v.'%\'';
						continue;
					}
					$where .= ' and '.$k.'='.$v;
				}
			}
		}
//		echo $where;
		$order = 'upd_time desc';
		return JYDB::findAll('QUESTIONNAIRE',$where,'*',$order,$page,$pagesize);
	}
	function getQuesnrTotalNum($select){
		if($this->unid<0){
			$where = 'state=3';
		}else{
			$where = 'state=3 and unit_id='.$this->unid;
		}
			if(count($select)>0){
				foreach ($select as $k=>$v){
					if(!empty($v)){
						if($k=='title'){
							$where .= ' and '.$k.' like \'%'.$v.'%\'';
							continue;
						}
						$where .= ' and '.$k.'='.$v;
					}
				}
			}
//			echo $where;
		return JYDB::findAll('QUESTIONNAIRE',$where,'count(*) num');
	}
	function getQuesnrCategory(){
		if($this->unid<0){
			$where = 'state=1 and cat_type=1';
		}else{
			$where = 'state=1 and unit_id='.$this->unid.' and cat_type=1';
		}
		
		$fields = 'cat_id cid,cat_name cn';
		$order = 'upd_time desc';
		$data = JYDB::findAll('CATEGORY',$where,$fields,$order);
		return $data;
	}
	function getRuleInfoById($id){
		if($this->unid<0){
			$where = 'r_id='.$id;
		}else{
			$where = 'unit_id='.$this->unid.' and r_id='.$id;
		}
		
		return JYDB::find($this->tableName,$where);
	}
	function getQuesnrInfoById($id){
		if(empty($id)){
			return ;
		}
		return JYDB::find('QUESTIONNAIRE','ques_id='.$id,'ques_id qid,title');
	}
	function optionRule($id,$state){
		$where = 'r_id='.$id;
		return JYDB::update($this->tableName,$where,array('state'=>$state));
	}
	function delRule($id){
		$where = 'r_id='.$id;
		return JYDB::update($this->tableName,$where,array('state'=>0));
	}
	function addVisitrule($arr){
		return JYDB::create($this->tableName,$arr,'R_ID');
	}
	function editVisitrule($id,$edit){
		$where = 'R_ID='.$id;
		return JYDB::update($this->tableName,$where,$edit);
	}
}